#pragma once
#include "afn0dbase.h"

#pragma pack(1)
class ActiveAfn0df1 : public Afn0dfbase
{
public:
	CommonA20 _time;
public:
	inline ActiveAfn0df1(uint16_t pn=0)
	:Afn0dfbase(1, pn)
	{
	}
	inline uint16_t length(void)
	{
		return sizeof(*this);
	}
	inline Mapss& get(Mapmss& mss)const
	{
		Mapss& m = mss[(int)pnfn];
		m["Fn"]   = Fn();
		m["Pn"]   = Pn();
		m["_time"] = _time.toStringU();
		return m;
	}
	inline bool set(const Mapss& m)
	{
		if(!m.have("Pn")
		|| !m.have("_time")
		|| !m.equal("Fn", Fn()))
		{
			return false;
		}
		int pn = 0;
		Mapss M = m;
		Pn(M["Pn"].toint());
		M["_time"] >> _time;
		return true;
	}
};

class PasiveAfn0df1 : public Afn0dfbase
{
	public:
		enum{RATE_MIN = 1, RATE_MAX = 12};
	public:
		CommonA20 _froztime;
		CommonA15 _copytime;
		uint8_t   _rate;
		CommonA14 _fwttap;			//forwardTotalActivePower;
		CommonA14 _fwap[RATE_MAX];	//forwardActivePower[RATE_MAX];
		CommonA11 _fwttrp;			//forwardTotalReactivePower;
		CommonA11 _fwrp[RATE_MAX];	//forwardReactivePower[RATE_MAX];
		CommonA11 _ps1ttrp;			//phase1TotalReactivePower;
		CommonA11 _ps1rp[RATE_MAX];	//phase1ReactivePower[RATE_MAX];
		CommonA11 _ps4ttrp;			//phase4TotalReactivePower;
		CommonA11 _ps4rp[RATE_MAX];	//phase4ReactivePower[RATE_MAX];
	public:
	inline PasiveAfn0df1(uint16_t pn=0)
	:Afn0dfbase(1,pn),_rate(RATE_MAX)
	{
	}
	inline void rate(uint8_t r)
	{
		if(r >= RATE_MIN && r <= RATE_MAX)
		{
			_rate = r;
		}
	}
	inline uint16_t length(void)const
	{
		if(_rate >= RATE_MIN && _rate <= RATE_MAX)
		{
			return sizeof(PasiveAfn0df1)
				 - (RATE_MAX - _rate) * sizeof(CommonA14)
				 - (RATE_MAX - _rate) * sizeof(CommonA11) 
				 - (RATE_MAX - _rate) * sizeof(CommonA11) 
				 - (RATE_MAX - _rate) * sizeof(CommonA11) 
				 ;
		}
	}
	inline Mapss& get(Mapmss& mss)const
	{
		Mapss& m = mss[(int)pnfn];
		return m;
	}
	inline bool set(const Mapss& m)
	{
		if(!m.have("Pn")
		|| !m.equal("Fn", 1)
		|| !m.have("_froztime")
		|| !m.have("_copytime")
		|| !m.have("_rate")	  
		|| !m.have("_fwttap")  
		|| !m.have("_fwttrp")  
		|| !m.have("_ps1ttrp") 
		|| !m.have("_ps4ttrp") )
		{
			cout << __func__ << "," << __LINE__ << endl;
			return false;
		}
		Mapss M = m;

		Pn(M["Pn"].toint());
		M["_froztime"] >> _froztime;
		M["_copytime"] >> _copytime;
		M["_rate"	 ] >> _rate;
		M["_fwttap"	 ] >> _fwttap;
		M["_fwttrp"	 ] >> _fwttrp;	
		M["_ps1ttrp" ] >> _ps1ttrp;
		M["_ps4ttrp" ] >> _ps4ttrp;
		for(int i = 0; i < _rate; i++)
		{
			sstream fwap;
			sstream fwrp;
			sstream ps1rp;
			sstream ps4rp;

			cout <<  i << endl;
			fwap  << "_fwap_"  << i;
			fwrp  << "_fwrp_"  << i;
			ps1rp << "_ps1rp_" << i;
			ps4rp << "_ps4rp_" << i;

			M[fwap.str() ] >> _fwap[i];	
			M[fwrp.str() ] >> _fwrp[i];
			M[ps1rp.str()] >> _ps1rp[i];
			M[ps4rp.str()] >> _ps4rp[i];
		}
		cout << hexBySize(_froztime) << endl;
		cout << hexBySize(_copytime) << endl;
		return true;
	}
};
#pragma pack(0)

